package com.young.commom.httpclient;

import com.young.common.expression.dns.NamingResolver;
import com.young.common.expression.dns.ThreadLocalResolverSupporter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @description：
 * @author: yangyahui01
 * @date: 11/29/24 12:39 PM
 */
public abstract class RequestHeaderInterceptor {
    protected NamingResolver dnsResolver;
    protected boolean mdcEnabled = true;
    protected Map<String, String> globalHeaders;

    public RequestHeaderInterceptor() {
    }

    public void setDnsResolver(NamingResolver dnsResolver) {
        this.dnsResolver = dnsResolver;
    }

    public void setMdcEnabled(boolean mdcEnabled) {
        this.mdcEnabled = mdcEnabled;
    }

    public void setGlobalHeaders(Map<String, String> globalHeaders) {
        this.globalHeaders = globalHeaders;
    }

    public Map<String, String> requestHeaders() {
        Map<String, String> result = new HashMap();
        if (this.globalHeaders != null && !this.globalHeaders.isEmpty()) {
            result.putAll(this.globalHeaders);
        }

        if (this.mdcEnabled) {
            result.putAll(this.mdcHeaders());
        }

        if (this.dnsResolver != null) {
            result.putAll(this.dnsResolverHeaders());
        }

        return result;
    }

    protected Map<String, String> mdcHeaders() {
        Map<String, String> headers = new HashMap();
        String requestId = MDC.get("requestId");
        if (requestId != null) {
            headers.put("X-Request-Id", requestId);
        }

        String requestSequence = MDC.get("requestSequence");
        if (requestSequence != null) {
            headers.put("X-Request-Sequence", requestSequence);
        }

        String invokeLink = MDC.get("meteorInvokeLink");
        if (invokeLink != null) {
            headers.put("X-Meteor-Invoke-Link", invokeLink);
        }

        String requestType = MDC.get("meteorRequestType");
        if (requestType != null) {
            headers.put("X-Meteor-Request-Type", requestType);
        }

        String specificRoot = MDC.get("Specific-Headers-Root");
        if (StringUtils.isNotBlank(specificRoot)) {
            String[] kvs = StringUtils.splitByWholeSeparator(specificRoot, "|");
            String[] var8 = kvs;
            int var9 = kvs.length;

            for(int var10 = 0; var10 < var9; ++var10) {
                String kv = var8[var10];
                if (!StringUtils.isEmpty(kv)) {
                    String name = StringUtils.substringBefore(kv, "=");
                    String value = StringUtils.substringAfter(kv, "=");
                    headers.put(name, value);
                }
            }
        }

        return headers;
    }

    protected Map<String, String> dnsResolverHeaders() {
        Map<String, String> headers = new HashMap();
        String content = this.transfer();
        if (content != null) {
            headers.put("X-Meteor-Dns-Resolver", content);
        }

        return headers;
    }

    protected String transfer() {
        Map<String, String> container = new HashMap();
        container.put("transmittable", "" + ThreadLocalResolverSupporter.getTransmittable().code);
        String dnsResolverDomain = this.domainContext();
        if (StringUtils.isNotEmpty(dnsResolverDomain)) {
            container.put("domain", dnsResolverDomain);
        }

        String dnsResolverService = this.serviceContext();
        if (StringUtils.isNotEmpty(dnsResolverService)) {
            container.put("service", dnsResolverService);
        }

        String dnsResolverServiceGroup = this.serviceGroupContext();
        if (StringUtils.isNotEmpty(dnsResolverServiceGroup)) {
            container.put("service-group", dnsResolverServiceGroup);
        }

        if (container.isEmpty()) {
            return null;
        } else {
            StringBuilder sb = new StringBuilder();
            container.forEach((key, value) -> {
                if (sb.length() > 0) {
                    sb.append(";");
                }

                sb.append(key).append("=>").append(value);
            });
            return sb.toString();
        }
    }

    private String convert(Map<String, String> mapper) {
        if (mapper == null) {
            return null;
        } else {
            StringBuilder sb = new StringBuilder();

            Map.Entry entry;
            for(Iterator var3 = mapper.entrySet().iterator(); var3.hasNext(); sb.append((String)entry.getKey()).append("=").append((String)entry.getValue())) {
                entry = (Map.Entry)var3.next();
                if (sb.length() > 0) {
                    sb.append(",");
                }
            }

            return sb.toString();
        }
    }

    private String domainContext() {
        Map<String, String> context = this.dnsResolver.copyDomainResolvers();
        return this.convert(context);
    }

    private String serviceContext() {
        Map<String, String> context = this.dnsResolver.copyServiceResolvers();
        return this.convert(context);
    }

    private String serviceGroupContext() {
        Map<String, String> context = this.dnsResolver.copyServiceGroupResolvers();
        return this.convert(context);
    }
}
